#!/bin/sh

e2e_test_install_pods() {
  echo 2
}

e2e_test() {
  run_test "Checking that after failover the primary is recovered if in another timeline" check_failover_recovery_after_new_timeline
}

check_failover_recovery_after_new_timeline() {
  run_query -i 0 -p 5432 -q "CREATE TABLE test(i int, s text)"
  wait_until run_query -i 1 -p 5432 -q "SELECT count(*) FROM test"

  echo "Setting restriction that simulate network split"
  cat << EOF | kubectl create -n "$CLUSTER_NAMESPACE" -f -
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: $CLUSTER_NAME
spec:
  podSelector:
    matchLabels:
      stackgres.io/cluster-namespace: $CLUSTER_NAMESPACE
      stackgres.io/cluster-name: $CLUSTER_NAME
      cluster: "true"
  policyTypes:
  - Ingress
  ingress:
  - ports:
    - protocol: TCP
      port: $POSTGRES_REPLICATION_PORT
EOF

  run_query -i 0 -p 5432 -q "INSERT INTO test SELECT i, 'try to generate a new timeline ' || i FROM generate_series(1, 10000) i"
  (
  while true
  do
    run_query -i 0 -p 5432 -q "INSERT INTO test SELECT i, 'try to generate a new timeline ' || i FROM generate_series(1, 10000) i" > "$LOG_PATH/inserts.log" 2>&1 \
      || true
  done
  ) &
  local PID=$!
  trap_kill "$PID"

  local RESULT
  local EXIT_CODE
  PATRONI_MAJOR_VERSION="$(kubectl exec -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-1" -c patroni -- \
    patronictl version 2>/dev/null | sed -n 's/^patronictl version \([0-9]\+\)\..*$/\1/p')"
  if [ "$PATRONI_MAJOR_VERSION" -lt 4 ]
  then
    try_function eval '
      sleep 1
      PATRONI_MAJOR_VERSION="$(kubectl exec -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-0" -c patroni -- \
        patronictl version 2>/dev/null | sed -n "s/^patronictl version \([0-9]\+\)\..*$/\1/p")"
      kubectl exec -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-1" -c patroni -- \
        patronictl failover \
          $([ "$PATRONI_MAJOR_VERSION" -lt 4 ] && printf %s --master || printf %s --primary) "$CLUSTER_NAME-0" \
          --candidate "$CLUSTER_NAME-1" --force'
  else
    try_function eval '
      sleep 1
      kubectl exec -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-1" -c patroni -- \
        patronictl failover --candidate "$CLUSTER_NAME-1" --force'
  fi
  if "$RESULT"
  then
    success "$CLUSTER_NAME-1 has been promoted to primary and $CLUSTER_NAME-0 has been removed abruptly!"
  else
    echo "FAILURE. $CLUSTER_NAME-1 has not been promoted to primary or $CLUSTER_NAME-0 has not been removed abruptly!"
  fi

  kill "$PID"

  echo "Removing restriction that simulate network split"
  kubectl delete networkpolicy -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME"

  if wait_until eval 'kubectl get pod -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-0" --template "{{ .metadata.labels.role }}" \
      | grep -qF "replica"'
  then
    success "Old primary has been converted to replica"
  else
    fail "Old primary has not been converted to replica"
  fi

  kubectl exec -n "$CLUSTER_NAMESPACE" "$CLUSTER_NAME-0" -c patroni \
    -- patronictl restart "$CLUSTER_NAME" "$CLUSTER_NAME-0" --force

  run_query -i 1 -p 5432 -q "INSERT INTO test SELECT i, 'try to generate a new timeline ' || i FROM generate_series(1, 10000) i"
  local EXPECTED_COUNT
  EXPECTED_COUNT="$(run_query -i 1 -p 5432 -q "SELECT COUNT(*) FROM test")"
  if wait_until eval '[ "$EXPECTED_COUNT" = "$(run_query -i 0 -p 5432 -q "SELECT COUNT(*) FROM test")" ]'
  then
    success "Old primary is replicating correctly from new primary"
  else
    fail_no_return "Old primary is not replicating correctly from new primary"
    echo "Expected count: $EXPECTED_COUNT"
    echo "Actual count: $(run_query -i 0 -p 5432 -q "SELECT COUNT(*) FROM test")"
    return 1
  fi
}
